<?php
/**
 * 商品模型
 * // 淘宝客推广列表 => 包含淘宝推广商品转换链接的商品列表
 * 
 * @version $Id: Item.php 294 2012-05-07 14:48:02Z linsir123 $
 */
class modelItem
{
	/**
	 * @var TOP数据请求对象
	 */
	public $top;
	
	/**
	 * 构造函数
	 */
	function __construct()
	{
		$this->top = M::topRequest();
	}
	
	/**
	 * 根据参数取得【淘宝客推广列表】
	 * // 通过`taobaokeItemsGet`
	 */
	public function taobaoke($params, $useUser = false)
	{
		$data = $this->top->taobaokeItemsGet($params);
		if ($useUser && is_array($data[0])) {
			$nicks = array();
			foreach ($data[0] as $v) 
				$nicks[] = $v['nick'];
				
			$users = $this->top->usersGet(implode(',', $nicks));
			$data[3] = $users;
		}
		
		return $data;
	}
	
	/**
	 * 根据商品Id取得【淘宝客推广列表】
	 * // 通过`taobaokeItemsConvert`
	 */
	public function taobaokeByIids($iids, $format = true)
	{
		$list = array();
		if ( ! empty($iids) && is_array($iids)) {
			$converts = $this->top->taobaokeItemsConvert(implode(',', $iids));
			if ( ! $format) 
				return $converts;
				
			///
			$data = array();
			foreach ($converts as $v) {
				$iid = strval($v['num_iid']);
				$data[$iid] = $v;
			}
				
			///
			foreach ($iids as $i => $iid) 
				$list[$i] = $data[$iid];
		}
		
		return $list;
	}
	
	/**
	 * 根据商铺取得【淘宝客推广列表】
	 * // 通过`taobaokeItemsConvert`
	 */
	public function taobaokeByShop($params)
	{
		$data = $this->top->itemsSearch($params);
		
		/// 列表
		$list = $data[0];
		$iids = array();
		if (is_array($list) && count($list) > 0) {
			foreach ($list as $val) 
				$iids[] = $val['iid'];
		}
		$data[0] = $this->top->taobaokeItemsConvert(implode(',', $iids));
		
		/// 分类
		$cates = $data[3];
		$cids = array();
		if (is_array($cates) && count($cates) > 0) {
			foreach ($cates as $val) 
				$cids[] = $val['category_id'];
		}
		$tmp = $this->top->itemcatsGet(implode(',', $cids));
		foreach ($tmp as $k => $var) 
			$tmp[$k]['count'] = $cates[$k]['count'];
		
		///
		$data[3] = $tmp;
		return $data;
	}
	
	/**
	 * 根据商品Id取得商品【详细信息】
	 * // 过滤描述内容的图片和链接
	 */
	public function getInfo($iid)
	{
		$info = $this->top->itemGet($iid);
		
		///
		// $pattern = "%<(img|a)[^>]*>%";
		// $info['desc'] = preg_replace($pattern, '', $info['desc']);
		
		///
		$propsName = array();
		$list = explode(';', $info['props_name']);
		foreach ($list as $v) 
			$propsName[] = preg_replace("/[\d]+:[\d]+:/", '', $v);
			
		$info['props_name'] = $propsName;
		return $info;
	}
	
	/**
	 * 根据商品Id取得商品【推广信息】
	 */
	public function getTaobaoke($iid)
	{
		$iid = strval($iid);
		$tmp = md5($iid);
		$file = CACHE_DIR.'taobao/_items/'.$tmp[0].'/'.$tmp[1].'/'.$iid.'.php';
		if (file_exists($file)) 
			return require($file);
			
		///
		$data = $this->top->taobaokeItemsConvert($iid);
		if (isset($data[0])) 
			return $data[0];
			
		return false;
	}
	
	/**
	 * 取得首页的推荐列表
	 */
	public function getHomeList()
	{
		$list = array();
		$tmp = App::O('_home.item');
		foreach ($tmp as $i) {
			$cid = $i['cid'];
			$show = $i['show'];
			$title = $i['title'];
			$keyword = $i['keyword'];
			
			///
			if ( ! empty($i['iids']) && is_array($i['iids'])) 
				$items = $this->taobaokeByIids($i['iids']);
			else {
				$params = array('show'=>$show,'keyword'=>$keyword,'cid'=>$cid,'sort'=>'commissionVolume_desc');
				$data = $this->taobaoke($params);
				list($items) = $data;
			}
			
			$keywords = M::keyword()->getList($cid);
			$list[] = array('cid'=>$cid, 'title'=>$title, 'items'=>$items, 'keywords'=>$keywords);
		}
		
		return $list;
	}
}
